package org.ithang.application.btRecord;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ithang.application.account.service.AccountService;
import org.ithang.application.btRecord.bean.BtRecord;
import org.ithang.application.btRecord.service.BtRecordService;
import org.ithang.application.charge.service.ChargeService;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.lang.StrUtils;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 提币记录
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/btRecord")
public class BtRecordAction extends Action<BtRecord>{

    @Autowired
    private BtRecordService btRecordService; 

    @Autowired
    private KeyvalueService keyvalueService;
    
    @Autowired
    private ChargeService chargeService;
    
    @Autowired
    private AccountService accountService;
    
    /**
     * 开始提币
     * @param btRecord
     * @return
     */
    @ResponseBody
   	@RequestMapping(value="pass",method=RequestMethod.POST)
   	public ActionResult pass(BtRecord btRecord,@RequestParam("prik")String prik,String ids)throws Exception{
    	if(StrUtils.isNotBlank(ids)){
    		Thread pass=new Thread(new Runnable() {
				
				@Override
				public void run() {
					String[] _ids=ids.split(",");
		    		for(String id:_ids){
		    			try{
		    			   BtRecord bt=new BtRecord();
		    			   bt.setId(Integer.parseInt(id));
		    			   bt.setResult(2);
		    			   btRecordService.pass(bt, prik);
		    			}catch(Exception e){
		    				e.printStackTrace();
		    			}
		    		}
				}
			});
    		pass.start();
    	}
    	
    	if(null!=btRecord&&null!=btRecord.getId()&&btRecord.getId()>0){
    		btRecordService.pass(btRecord, prik);
    	}
    	
   		return success();
   	}
    

	@RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)String id,@RequestParam(value="page")Integer page,Model m){
    	if(null!=id){
    		if(!id.contains(",")){
    			BtRecord bean=btRecordService.get(Integer.parseInt(id));
        		m.addAttribute(bean);
    		}
    		
    		if(id.contains(",")){
    			Map<String,Object> values=new HashMap<>();
    			values.put("ids", id);
    			m.addAllAttributes(values);
    		}
    	}else{
    		m.addAttribute(new BtRecord());
    	}
    	if(0==page){
    		return "app/btRecord/pass";
    	}
    	if(1==page){
    		return "app/btRecord/refuse";
    	}
    	return "404";
    }
	
	
	
	@ResponseBody
	@RequestMapping(value="refuse",method=RequestMethod.POST)
	public ActionResult refuse(BtRecord btRecord)throws Exception{
		btRecordService.refuse(btRecord);
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(BtRecord btRecord){
		btRecordService.update(btRecord);
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Integer id){
		BtRecord r=btRecordService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Integer id,@RequestParam(value="ids",required=false)String ids){
		
		if(null!=id&&id>0){
			btRecordService.delete(id);	
		}
		
		if(null!=ids&&ids.length()>0){
			btRecordService.batchDelete(ids.split(","));
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Integer... ids){
		return success(btRecordService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(BtRecord btRecord,Pager<BtRecord> page){
	    page.setBean(btRecord);
	    List<BtRecord> data=btRecordService.page(page);
	    page.setData(data);
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(btRecordService.query(values));
	}
     

}